package com.github.tommas.admintpl.config;

import com.github.tommas.admintpl.bean.vo.AuthorizationInfo;
import com.github.tommas.admintpl.cache.CacheFactory;
import com.github.tommas.admintpl.cache.CacheManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class CacheConfiguration {
    public static final String DEPT_ISOLATION_CACHE = "entity_isolation_cache";
    public static final String AUTHORIZATION_INFO_CACHE = "authorization_info_cache";

    @SuppressWarnings("unchecked")
    @Bean(name = DEPT_ISOLATION_CACHE)
    CacheManager<List<Integer>> deptIsolationCacheManager(CacheFactory cacheFactory,
                                                            @Value("${cache.instance.enable-dept-isolation:false}")
                                                            boolean enableEntityIsolation) {
        return enableEntityIsolation ? new CacheManager<>(cacheFactory) : CacheManager.NOOP_CACHE_MANAGER;
    }

    @SuppressWarnings("unchecked")
    @Bean(name = AUTHORIZATION_INFO_CACHE)
    CacheManager<AuthorizationInfo> authorizationInfoCacheManager(CacheFactory cacheFactory,
                                                                  @Value("${cache.instance.enable-authorization-info:false}")
                                                                  boolean enableAuthorizationInfoCache) {
        return enableAuthorizationInfoCache ? new CacheManager<>(cacheFactory) : CacheManager.NOOP_CACHE_MANAGER;
    }
}
